package top.mcwebsite.markdown.render

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.findChildOfType
import top.mcwebsite.markdown.MarkdownState
import top.mcwebsite.markdown.model.MarkdownNode
import top.mcwebsite.markdown.theme.LocalMarkdownTheme


object OrderedListComponent : MarkdownListComponent {

    @Composable
    override fun render(markdownNode: MarkdownNode, level: Int) {
        val theme = LocalMarkdownTheme.current
        Column(
            modifier = Modifier.padding(theme.blockPadding)
        ) {
            var index = 1
            markdownNode.node.children
                .filter { it.type == MarkdownElementTypes.LIST_ITEM }
                .forEach {
                    OrderedListItem(
                        node = it,
                        content = markdownNode.content,
                        state = markdownNode.state,
                        renders = markdownNode.renders,
                        level = level,
                        index = index++
                    )
                }
        }
    }

    @Composable
    fun OrderedListItem(
        node: ASTNode,
        content: String,
        state: MarkdownState,
        renders: Map<IElementType, MarkdownComponent>,
        level: Int,
        index: Int,
    ) {
        val theme = LocalMarkdownTheme.current
        Column(modifier = Modifier.padding(theme.orderListTheme.itemPadding)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = theme.orderListTheme.levelIndexToString(level, index) + ".")
                node.findChildOfType(MarkdownElementTypes.PARAGRAPH)?.let {
                    MarkdownText(
                        content = content,
                        astNode = it,
                        markdownState = state,
                        textStyle = theme.text,
                        modifier = Modifier.padding(start = theme.orderListTheme.indexContentSpace)
                    )
                }
            }
            node.findChildOfType(MarkdownElementTypes.ORDERED_LIST)?.let {
                Row {
                    Spacer(modifier = Modifier.padding(start = theme.orderListTheme.nextLevelSpace))
                    render(MarkdownNode(it, content, state, renders), level + 1)
                }
            }
            node.findChildOfType(MarkdownElementTypes.UNORDERED_LIST)?.let {
                Row {
                    Spacer(modifier = Modifier.padding(start = theme.orderListTheme.nextLevelSpace))
                    (renders[MarkdownElementTypes.UNORDERED_LIST] as? MarkdownListComponent)
                        ?.render(MarkdownNode(it, content, state, renders), level + 1)
                }
            }
        }
    }
}